//
//  JFLapolicyHelper.m
//  JFBase
//
//  Created by Alan Wang on 2019/7/26.
//  Copyright © 2019 Alan Wang. All rights reserved.
//

#import "JFLapolicyHelper.h"
#import "JFBase.h"
#import "JFBasePri.h"

@implementation JFLapolicyHelper
+(JFLapolicyType)supportLapolicyType{
    if (NSFoundationVersionNumber < NSFoundationVersionNumber_iOS_8_0) {
        return JFLapolicyType_None;
    }else{
        if ([JFDeviceInfo isFullScreen]) {
            return JFLapolicyType_FaceId;
        }else{
            return JFLapolicyType_TouchId;
        }
    }
}
+(void)checkLaPolicyState:(NSString*)localizedReason fallbackTitle:(NSString*)fallbackTitle stateBlock:(JFLapolicyHelperStateBlock)block{
    NSString * tempLocalizedReason = localizedReason;
    if ([JFStringHelper isEmpty:localizedReason]) {
        if ([JFDeviceInfo isFullScreen]) {
            tempLocalizedReason = JFBaseLocalizedString(@"localizedReasonFaceId");
        }else{
            tempLocalizedReason = JFBaseLocalizedString(@"localizedReasonTouchId");
        }
    }
      NSError *error = nil;
    switch ([self supportLapolicyType]) {
        case JFLapolicyType_None:
        {
            if (block) {
                error = [[NSError alloc] initWithDomain:@"" code:-1 userInfo:@{@"msg":@"系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)"}];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (block) {
                         block(JFLapolicyState_VersionNotSupport,error,@"系统版本不支持TouchID/FaceID (必须高于iOS 8.0才能使用)");
                    }
                });
            }
            
        }break;
        case JFLapolicyType_FaceId:
        case JFLapolicyType_TouchId:
        {
            LAContext *context = [[LAContext alloc] init];
            if ([JFStringHelper isNotEmpty:fallbackTitle]) {
                context.localizedFallbackTitle = fallbackTitle;
            }
            
            if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
                [context evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:tempLocalizedReason reply:^(BOOL success, NSError * _Nullable error) {
                    JFLapolicyState state = JFLapolicyState_Failed;
                    NSString * msg = @"";
                    if (success) {
                        state = JFLapolicyState_Success;
                        msg = @"TouchID/FaceID 验证成功";
                    }else if(error){
                        
                        if (@available(iOS 11.0, *)) {
                            switch (error.code) {
                                case LAErrorAuthenticationFailed:{
                                    state = JFLapolicyState_Failed;
                                    msg = @"TouchID/FaceID 验证失败";
                                    break;
                                }
                                case LAErrorUserCancel:{
                                    state = JFLapolicyState_UserCancel;
                                    msg = @"TouchID/FaceID 被用户手动取消";
                                }
                                    break;
                                case LAErrorUserFallback:{
                                    state = JFLapolicyState_InputPassword;
                                    msg = @"用户不使用TouchID/FaceID,选择手动输入密码";
                                }
                                    break;
                                case LAErrorSystemCancel:{
                                    state = JFLapolicyState_SystemCancel;
                                    msg = @"TouchID/FaceID 被系统取消 (如遇到来电,锁屏,按了Home键等)";
                                }
                                    break;
                                case LAErrorPasscodeNotSet:{
                                    state = JFLapolicyState_PasswordNotSet;
                                    msg = @"TouchID/FaceID 无法启动,因为用户没有设置密码";
                                }break;
                                case LAErrorBiometryNotEnrolled:{
                                    state = JFLapolicyState_NotEnrolled;
                                    msg = @"TouchID/FaceID 无法启动,因为用户没有设置TouchID/FaceID";
                                }break;
                                case LAErrorBiometryNotAvailable:{
                                    state = JFLapolicyState_NotAvailable;
                                    msg = @"TouchID/FaceID 无效";
                                }break;
                                case LAErrorBiometryLockout:{
                                    state = JFLapolicyState_Lockout;
                                    msg = @"TouchID/FaceID 被锁定(连续多次验证TouchID/FaceID失败,系统需要用户手动输入密码)";
                                }
                                    break;
                                case LAErrorAppCancel:{
                                    state = JFLapolicyState_AppCancel;
                                    msg = @"当前软件被挂起并取消了授权 (如App进入了后台等)";
                                }
                                    break;
                                case LAErrorInvalidContext:{
                                    state = JFLapolicyState_InvalidContext;
                                    msg = @"当前软件被挂起并取消了授权 (LAContext对象无效)";
                                }
                                    break;
                                default:
                                    break;
                            }
                        } else {
                            // iOS 11.0以下的版本只有 TouchID 认证
                            switch (error.code) {
                                case LAErrorAuthenticationFailed:{
                                    state = JFLapolicyState_Failed;
                                    msg = @"TouchID 验证失败";
                                    break;
                                }
                                case LAErrorUserCancel:{
                                    state = JFLapolicyState_UserCancel;
                                    msg = @"TouchID 被用户手动取消";
                                }
                                    break;
                                case LAErrorUserFallback:{
                                    state = JFLapolicyState_InputPassword;
                                    msg = @"用户不使用TouchID,选择手动输入密码";
                                }
                                    break;
                                case LAErrorSystemCancel:{
                                    state = JFLapolicyState_SystemCancel;
                                    msg = @"TouchID 被系统取消 (如遇到来电,锁屏,按了Home键等)";
                                }
                                    break;
                                case LAErrorPasscodeNotSet:{
                                    state = JFLapolicyState_PasswordNotSet;
                                    msg = @"TouchID 无法启动,因为用户没有设置密码";
                                }break;
                                case LAErrorTouchIDNotEnrolled:{
                                    state = JFLapolicyState_NotSet;
                                    msg = @"TouchID 无法启动,因为用户没有设置TouchID";
                                }
                                    break;
                                case LAErrorTouchIDNotAvailable:{
                                    state = JFLapolicyState_NotAvailable;
                                    msg = @"TouchID 无效";
                                }
                                    break;
                                case LAErrorTouchIDLockout:{
                                    state = JFLapolicyState_Lockout;
                                    msg = @"TouchID 被锁定(连续多次验证TouchID失败,系统需要用户手动输入密码)";
                                }
                                    break;
                                case LAErrorAppCancel:{
                                    state = JFLapolicyState_AppCancel;
                                    msg = @"当前软件被挂起并取消了授权 (如App进入了后台等)";
                                }
                                    break;
                                case LAErrorInvalidContext:{
                                    state = JFLapolicyState_InvalidContext;
                                    msg = @"当前软件被挂起并取消了授权 (LAContext对象无效)";
                                }
                                    break;
                                default:
                                    break;
                            }
                        }
                        
                    }
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (block) {
                             block(state,error,msg);
                        }
                    });
                }];
                
            }else{
                 error = [[NSError alloc] initWithDomain:@"" code:-2 userInfo:@{@"msg":@"当前设备不支持TouchID/FaceID"}];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (block) {
                       block(JFLapolicyState_NotSupport,error,@"当前设备不支持TouchID/FaceID");
                    }
                });
            }
        }break;
            
        default:
            break;
    }
    
}
@end
